package com.dataseek.iot.device.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dataseek.iot.device.entity.Question;
import com.dataseek.iot.device.entity.QuestionImage;
import com.dataseek.iot.device.mapper.QuestionImageMapper;
import com.dataseek.iot.device.service.QuestionService;
import com.dataseek.iot.device.vo.QuestionVo;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.toolikit.common.response.ResponseResult;
import com.dataseek.iot.toolikit.common.service.IDServiceHelper;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("**/question")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @Autowired
    private IDServiceHelper idServiceHelper;

    @Autowired
    private QuestionImageMapper questionImageMapper;

    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ResponseResult query(@RequestParam(value="page")  Integer page,
                                      @RequestParam(value="size")  Integer size,
                                      @RequestParam(value="userId", required=false) String userId,
                                      @RequestParam(value="status", required=false) String status,
                                      @RequestParam(value="title", required=false) String title) throws IOException {
        QuestionVo tenantVo = questionService.queryAction(userId, status, title, page,size);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功",tenantVo);
    }

    @RequestMapping(value = "/qryById", method = RequestMethod.GET)
    public ResponseResult qryById(@RequestParam(value="id", required=true) Long id) throws IOException {
        Question question=questionService.qryById(id);
        //挂载图片
        QueryWrapper<QuestionImage> imageQw=new QueryWrapper();
        imageQw.eq("question_id",question.getId());
       List<QuestionImage> questionImageList= questionImageMapper.selectList(imageQw);
       question.setQuestionImageList(questionImageList);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功",question);
    }


    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public ResponseResult insert(@RequestBody Question question,@RequestParam(value="imageUrls", required=false) String imageUrls) throws IOException {

        //新增问题
        long questionId=idServiceHelper.nextId();
        question.setId(questionId);
        Integer code  = questionService.insertAction(question);
        //新增问题图片关联关系
        String[] imageFiles=imageUrls.split(",");
        QuestionImage questionImage=new QuestionImage();
        questionImage.setQuestionId(questionId);
        for(String url:imageFiles) {
            long imageId=idServiceHelper.nextId();
            questionImage.setQuestionImagesUrl(url);
            questionImage.setImagesId(imageId);
            String[] ss1=url.split("/");
            String title=ss1[ss1.length-1];
            questionImage.setQuestionTitle(title);
            questionImageMapper.insert(questionImage);
        }
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功!",code.toString());
    }


    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseResult update(@RequestBody Question question) throws IOException {
        Integer code  = questionService.updateAction(question);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),code.toString());
    }

    @ApiOperation("返回大屏问题类型饼图数据")
    @RequestMapping(value = "/queryTypeNum", method = RequestMethod.GET)
    public ResponseResult queryTypeNum() throws IOException {
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功", questionService.queryTypeNum());
    }

}
